import { calcWordWidth } from '@/utils/calcWordWidth';
const setEventsListener = (lf:any,params:any) => {
    const { eventCenter } = lf.graphModel;
    eventCenter.on("node:dbclick", (args:any) => {         
        if (params.userEditNodeRef.value && params.userEditNodeRef.value.editNode && (args.data.type === 'userTask' || args.data.type === 'systemTask' || args.data.type === 'gateway')) {
            params.userEditNodeRef.value.editNode(args.data);
            lf.selectElementById(args.data.id,false,true);
        }else if(params.userEditNodeRef.value && params.userEditNodeRef.value.editNode && args.data.type === 'custText'){                
            lf.selectElementById(args.data.id,false,true);
        }
    }); 
    eventCenter.on("edge:dbclick", (args:any) => {               
        if (params.userEditNodeRef.value && params.userEditNodeRef.value.editNode && (args.data.type === 'custom-edge')) {
            params.userEditNodeRef.value.editNode(args.data);
            lf.selectElementById(args.data.id,false,true);
        }
    }); 
    eventCenter.on("node:delete", (args:any) => {            
        if(args.data.type === 'custText' && args.data.properties.associatedId){
            lf.setProperties(args.data.properties.associatedId,{associatedId:''});
        }
    }); 
    eventCenter.on("edge:delete", (args:any) => {                           
        if(args.data.type === 'custom-edge' && args.data.properties.associatedId){
            lf.deleteNode(args.data.properties.associatedId);
        }
    }); 
    eventCenter.on("node:mousedown", (args:any) => {                        
        lf.selectElementById(args.data.id,false,true);
    }); 

    eventCenter.on("custNode:change", (args:any) => {  
        const nodeModel = lf.getNodeModelById(args.id);
        if(nodeModel){
            const x = nodeModel.x
            const width  = nodeModel.properties.width
            const textWidth = calcWordWidth(args.customNodeName) + 14;
            // nodeModel.updateAttributes({width: args.width, customNodeName: args.customNodeName}); 
            lf.setProperties(args.id,{
                width: textWidth, 
                customNodeName: args.customNodeName,
            });
            nodeModel.x = x + (textWidth - width)/2
            nodeModel.draggable = true;
            if(nodeModel.properties.associatedId){
                lf.setProperties(nodeModel.properties.associatedId,{                    
                    customNodeName: args.customNodeName,
                });
            }
        }                     
    }); 
    eventCenter.on("custNode:onchanging", (args:any) => {
        const nodeModel = lf.getNodeModelById(args.id);
        if(nodeModel){
            const x = nodeModel.x
            const width  = nodeModel.properties.width     
            const textWidth = calcWordWidth(args.customNodeName) + 14
            const inputWidth = textWidth>=100?textWidth:100;                                         
            lf.setProperties(args.id,{
                width: inputWidth,                     
            });
            nodeModel.x = x + (inputWidth - width)/2 
            nodeModel.draggable = false;                
        }                              
    }); 
}

export default setEventsListener;